package com.cxyd.modules.job.controller;

import com.cxyd.common.constant.Constants;
import com.cxyd.common.page.PageData;
import com.cxyd.common.utils.Result;
import com.cxyd.common.utils.StringUtils;
import com.cxyd.modules.job.dto.SysJobDTO;
import com.cxyd.modules.job.service.SysJobService;
import com.cxyd.modules.job.utils.CronUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/sys/job")
public class SysJobController {
    private final SysJobService jobService;

    public SysJobController(SysJobService jobService) {
        this.jobService = jobService;
    }

    @GetMapping("page")
    @RequiresPermissions("sys:schedule:page")
    public Result<PageData<SysJobDTO>> page(@RequestParam Map<String, Object> params) {
        PageData<SysJobDTO> page = jobService.page(params);
        return new Result<PageData<SysJobDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @RequiresPermissions("sys:schedule:info")
    public Result<SysJobDTO> info(@PathVariable("id") Long id) {
        SysJobDTO job = jobService.get(id);

        return new Result<SysJobDTO>().ok(job);
    }

    @PostMapping
    @RequiresPermissions("sys:schedule:save")
    public Result<Void> save(@RequestBody SysJobDTO job) {
        if (!CronUtils.isValid(job.getCronExpression())) {
            return new Result<Void>().error(500, "新增任务'" + job.getJobName() + "'失败，Cron表达式不正确");
        } else if (StringUtils.containsIgnoreCase(job.getInvokeTarget(), Constants.LOOKUP_RMI)) {
            return new Result<Void>().error(500, "新增任务'" + job.getJobName() + "'失败，目标字符串不允许'rmi'调用");
        } else if (StringUtils.containsAnyIgnoreCase(job.getInvokeTarget(), new String[]{Constants.LOOKUP_LDAP, Constants.LOOKUP_LDAPS})) {
            return new Result<Void>().error(500, "新增任务'" + job.getJobName() + "'失败，目标字符串不允许'ldap(s)'调用");
        } else if (StringUtils.containsAnyIgnoreCase(job.getInvokeTarget(), new String[]{Constants.HTTP, Constants.HTTPS})) {
            return new Result<Void>().error(500, "新增任务'" + job.getJobName() + "'失败，目标字符串不允许'http(s)'调用");
        } else if (StringUtils.containsAnyIgnoreCase(job.getInvokeTarget(), Constants.JOB_ERROR_STR)) {
            return new Result<Void>().error(500, "新增任务'" + job.getJobName() + "'失败，目标字符串存在违规");
        }
        jobService.save(job);
        return new Result<Void>();
    }

    @PutMapping
    @RequiresPermissions("sys:schedule:update")
    public Result update(@RequestBody SysJobDTO job) {
        if (!CronUtils.isValid(job.getCronExpression())) {
            return new Result().error(500, "修改任务'" + job.getJobName() + "'失败，Cron表达式不正确");
        } else if (StringUtils.containsIgnoreCase(job.getInvokeTarget(), Constants.LOOKUP_RMI)) {
            return new Result().error(500, "修改任务'" + job.getJobName() + "'失败，目标字符串不允许'rmi'调用");
        } else if (StringUtils.containsAnyIgnoreCase(job.getInvokeTarget(), new String[]{Constants.LOOKUP_LDAP, Constants.LOOKUP_LDAPS})) {
            return new Result().error(500, "修改任务'" + job.getJobName() + "'失败，目标字符串不允许'ldap(s)'调用");
        } else if (StringUtils.containsAnyIgnoreCase(job.getInvokeTarget(), new String[]{Constants.HTTP, Constants.HTTPS})) {
            return new Result().error(500, "修改任务'" + job.getJobName() + "'失败，目标字符串不允许'http(s)'调用");
        } else if (StringUtils.containsAnyIgnoreCase(job.getInvokeTarget(), Constants.JOB_ERROR_STR)) {
            return new Result().error(500, "修改任务'" + job.getJobName() + "'失败，目标字符串存在违规");
        }
        jobService.update(job);

        return new Result();
    }


    @DeleteMapping
    @RequiresPermissions("sys:schedule:delete")
    public Result delete(@RequestBody Long[] ids) {
        jobService.deleteBatch(ids);
        return new Result();
    }

    @PutMapping("/pause")
    @RequiresPermissions("sys:schedule:pause")
    public Result pause(@RequestBody Long[] ids) {
        jobService.pause(ids);
        return new Result();
    }

    @PutMapping("/resume")
    @RequiresPermissions("sys:schedule:resume")
    public Result resume(@RequestBody Long[] ids) {
        jobService.resume(ids);
        return new Result();
    }
}
